//
//  BookstoreViewController.swift
//  SLQuickReader
//
//  Created by ALin on 2017/8/2.
//  Copyright © 2017年 ALin. All rights reserved.
//

import UIKit
import Then

class BookstoreViewController: BaseViewController {
    var classificationView: UIView!
    var rankView: UIView!
    var recommendView: UIView!

    
    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.titleView = segmentedControl
        
        setupChildren(RecommendViewController(), arg: "recommendView")
        setupChildren(RankingViewController(), arg: "rankView")
        setupChildren(ClassificationViewController(), arg: "classificationView")
    }
    
    fileprivate func setupChildren(_ child: UIViewController, arg: String){
        addChildViewController(child)
        child.view.frame = view.bounds
        view.addSubview(child.view)
        setValue(child.view, forKey: arg)
    }
    
    @objc fileprivate func selectSegmentedControl(){
        // 隐藏导航栏的分割线
        navigationController?.navigationBar.subviews.first?.isHidden = segmentedControl.selectedSegmentIndex == 1
        switch segmentedControl.selectedSegmentIndex {
        case 0:
            view.bringSubview(toFront: classificationView)
        case 1:
            view.bringSubview(toFront: rankView)
        case 2:
            view.bringSubview(toFront: recommendView)
        default:
            break
        }
    }
   
    private lazy var segmentedControl : UISegmentedControl = {
        let s = UISegmentedControl(items: [local("book_store_g"), local("book_store_rank"), local("book_store_new")]).then{
            $0.selectedSegmentIndex = 0
            $0.width = bookstore_segmentedControl_w
            $0.addTarget(self, action: #selector(selectSegmentedControl), for: .valueChanged)
        }
        return s
    }()

}
